package ml.kupin.service.impl;

import ml.kupin.common.jedis.JedisClient;
import ml.kupin.common.vo.EasyUITreeNode;
import ml.kupin.mapper.TbItemCatMapper;
import ml.kupin.pojo.TbItemCat;
import ml.kupin.pojo.TbItemCatExample;
import ml.kupin.service.ItemCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品分类管理
 * Created by 小柯(Allen Kerr) on 17.4.25 9:24.
 */
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Autowired
    private JedisClient jedisClient;

    @Value("${item_cat_hash_key}")
    private String item_cat_hash_key;
    @Value("${item_cat_hash_lifetime}")
    private int item_cat_hash_lifetime;

    /**
     * 依据 parentId 获取商品分类列表，
     * 并转换为 EasyUITreeNode集合返回
     *
     * @param parentId
     * @return
     */
    @Override
    public List<EasyUITreeNode> getItemCategories(Long parentId) {
        //查询商品分类列表
        TbItemCatExample itemCatExample = new TbItemCatExample();
        //查询条件设置
        itemCatExample.createCriteria().andParentIdEqualTo(parentId);
        //执行查询
        List<TbItemCat> itemCats = itemCatMapper.selectByExample(itemCatExample);
        //转换为 EasyUITreeNode集合
        List<EasyUITreeNode> treeNodeList = new ArrayList<>();
        for (TbItemCat itemCat : itemCats) {
            EasyUITreeNode treeNode = new EasyUITreeNode();
            treeNode.setId(itemCat.getId());
            treeNode.setText(itemCat.getName());
            treeNode.setState(itemCat.getIsParent() ? "closed" : "open");
            //添加到treeNodeList
            treeNodeList.add(treeNode);
        }
        //返回结果
        return treeNodeList;
    }

    @Override
    public Map<String, String> getItemCategoriesMap() {
        //先在redis缓存中找
        try {
            //jedisClient.hgetAll(key)方法忘记加jedis.close()方法释放资源！
            //导致连续下载7次左右，就会出现连接池满的问题
            if (jedisClient.exists(item_cat_hash_key)) {
                Map<String, String> map = jedisClient.hgetAll(item_cat_hash_key);
                //重设生存时间
                jedisClient.expire(item_cat_hash_key, item_cat_hash_lifetime);
                return map;
            }
            /*Map<String, String> map = jedisClient.hgetAll(item_cat_hash_key);
            if (map != null && map.size() > 0) {
                //重设生存时间
                jedisClient.expire(item_cat_hash_key, item_cat_hash_lifetime);
                return map;
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        //redis缓存中找不到，再去数据库中找
        List<TbItemCat> itemCatList = itemCatMapper.selectByExample(new TbItemCatExample());
        Map<String, String> map = new HashMap<>();
        for (TbItemCat itemCat : itemCatList) {
            map.put(itemCat.getId().toString(), itemCat.getName());
            //将找到的数据存入缓存中
            try {
                jedisClient.hset(item_cat_hash_key, itemCat.getId().toString(), itemCat.getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            jedisClient.expire(item_cat_hash_key, item_cat_hash_lifetime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
